"use strict";

const BaseModel = require("../basics/mysql/mapper");
const Constant = require("../basics/constant");
class HomeModel extends BaseModel {
  constructor(cloud) {
    super(cloud, "game_power");
  }

  /**
   * 获取实例
   * @param {*} cloud 
   */
  static getInstant(cloud) {
    return new HomeModel(cloud);
  }


  /**
   * 查询单条
   * @param {*} openId 
   * @param {*} code 
   */
  async selectOne(openId, today) {
    const ex = this.createExample();
    ex.criteria.andEqual("openId", openId).andEqual("today", today);
    let data = await this.selectOneByExample(ex);
    if (!data) {
      data = { openId, today, total: 1, stock: 1 };
      try {
        data.id = await this.insertOne(data);
      } catch (error) {
        data = await this.selectOneByExample(ex);
      }
    }
    return data;
  }

  /**
   * 添加
   * @param {*} id 
   */
  async increased(id) {
    const sql = `update game_power set total=total+1,stock=stock+1 where id=${id}`;
    return await this.mfSql(sql, []);
  }

}

module.exports = HomeModel;